 
import HelloWorld from "@/components/HelloWorld"

Vue.use(VueRouter) 


const router = new VueRouter({
  routes: [
    {
      path: "/login",
      name: "login",
      component: ()=>import("@/views/login/index"),
      meta: { requiresAuth: false }
    },
    {
      path: "/",
      name: "homeIndex",
      component: ()=>import("@/views/home/index"),

      meta: { requiresAuth: true },
      children:[
        {
          path: "/home/tendency",
          name: "tendency",
          component: ()=>import("@/views/home/tendency"),
          meta:{
            title:"项目趋势",
            requiresAuth: true 
          }
        },
        {
          path: "/home/todolist",
          name: "todolist",
          component: ()=>import("@/views/home/todolist"),
          meta:{
            title:"我的待办",
            requiresAuth: true 
          }
        },
        //小程序管理-首页设置
        {
          path: "/miniproHomeSetting",
          name: "miniproHomeSetting",
          component: ()=>import("@/views/minipro/HomeSetting/index"),
          meta:{
            title:"首页展示设置",
            requiresAuth: true 
          }
        },
        //小程序管理-首页-添加最新动态模块表单
        {
          path: "/editActiveImg",
          name: "editActiveImg",
          component: ()=>import("@/views/minipro/HomeSetting/activeImg"),
          meta:{
            title:"最新动态模块",
            requiresAuth: true 
          }
        },
        { 
          path: "/usercentersetting",
          name: "usercentersetting",
          component: ()=>import("@/views/minipro/userCenter/setting"),
          meta:{
            title:"个人中心",
            requiresAuth: true 
          } 
        },
        {
          path:'/showText',
          name:'showText',
          component:()=>import("@/views/minipro/userCenter/showText"),
          meta:{
            title:'服务条款设置',
            
          }
        },
        {
          path:'/addShop',
          name:'addShop',
          component:()=>import('@/views/minipro/userCenter/addShop'),
          meta:{
            title:'添加线下门店'
          }
        },
        {
          path:'/customer',
          name:'customer',
          component:()=>import('@/views/minipro/vipCustomer/customer'),
          meta:{
            title:'会员管理'
          }
        },
        { 
          path:'/customerDetail',
          name:'customerDetail',
          component:()=>import('@/views/minipro/vipCustomer/detail'),
          meta:{
            title:'会员详情'
          }
        },
        { 
          path:'/commodity',
          name:'commodity',
          component:()=>import('@/views/minipro/shop/commodity'),
          meta:{
            title:'商品管理'
          }
        },
        { 
          path:'/edit_commodity',
          name:'edit_commodity',
          component:()=>import('@/views/minipro/shop/editCommodity'),
          meta:{
            title:'商品编辑'
          }
        },

        {
          path: "/property",
          name: "property",
          component: ()=>import("@/views/property/index"),
          meta:{
            title:"资产管理/资产录入",
            requiresAuth: true 
          }
        },
        {
          path: "/property/add",
          name: "propertyAdd",
          component: ()=>import("@/views/property/add"),
          meta:{
            title:"资产管理/资产录入",
            requiresAuth: true 
          }
        },
        // {
        //   path: "/property/edit",
        //   name: "propertyEdit",
        //   component: ()=>import("@/views/property/add"),
        //   meta:{
        //     title:"资产管理/资产编辑",
        //     requiresAuth: true 
        //   }
        // },
        //客户
       
       
        //系统管理
        {
          path: "/system/userlist",
          name: "userlist",
          component: ()=>import("@/views/system/userlist"),
          meta:{
            title:"用户管理",
            requiresAuth: true 
          }
        },
        {
          path: "/system/character",
          name: "character",
          component: ()=>import("@/views/system/character"),
          meta:{
            title:"角色管理",
            requiresAuth: true 
          }
        },
        {
          path: "/system/jurisdiction",
          name: "jurisdiction",
          component: ()=>import("@/views/system/jurisdiction"),
          meta:{
            title:"权限管理",
            requiresAuth: true 
          }
        },
        {
          path: "/system/blockchain",
          name: "blockchain",
          component: ()=>import("@/views/system/blockchain"),
          meta:{
            title:"区块链管理",
            requiresAuth: true 
          }
        },
      ]
    },
  ]
})

/*路由拦截器设置-before*/
router.beforeEach((to, from, next) => { 
    let token = sessionStorage.getItem("token")
    //1.登录权限判断
    if(to.meta.requiresAuth) {
        //验证token，存在才跳转
        if(token) {

            next()
        } 
        else {
            next({
                path: "/login",
                query: { redirect: to.fullPath }
            })
        }
    } 
    else{
         if(to.path === "/" ) {
             /*后退到login时拦截*/
             if(token){
                 next({path:"/home/tendency"})
             }
             //根目录（刚进入页面时）
             else{
                 next({path: "/login"})
             }
         }
         else{
          // alert(to.path)
            next()
         }
    }
    /*2.非登陆条件下输入某个地址 需要先跳到登录页再跳转到相应页面*/
    if(to.matched.length == 0 ){
        next({path: "/login",query: { redirect: to.fullPath }})
    }
});

router.afterEach((to, from) => {
  // // 设置面包屑
  // let breadCrumbItems = []
  // let homePageCrumb = {
  //   title: "首页",
  //   to: "/"
  // }
  // if(to.meta.title == "首页"){

  // }
  // else{
  //   breadCrumbItems.push(homePageCrumb)
  // }
  
  // if(to.meta.title) {
  //   let fullPathSplit = to.meta.title.split("/")
  //   // console.log(fullPathSplit)
    
  //       let sec_path = "/"+to.path.split("/")[1]
  //       //二级目录 需要link
  //       if(sec_path == "/company"  || sec_path == "/examine" ){
  //             fullPathSplit.forEach(( item, index ) => {
  //               let routerBreadCrumb = {
  //                 title: item,
  //                  to: (index == 0 ? sec_path : "" )
  //                  // to: (index == fullPathSplit.length - 1 ? to.path : "" )
                  
  //               }
  //               breadCrumbItems.push(routerBreadCrumb)
  //             });
  //       }
  //       //二级目录 需要为空
  //       else{
  //             fullPathSplit.forEach(( item, index ) => {
  //               let routerBreadCrumb = {
  //                 title: item,
  //                  to: (index == 0 ? "" : ( index == fullPathSplit.length - 1 ? to.path : sec_path) )
  //                  // to: (index == fullPathSplit.length - 1 ? to.path : "" )
                  
  //               }
  //               breadCrumbItems.push(routerBreadCrumb)
  //             });
  //       }
        
  //   // console.log(breadCrumbItems) 

  // }
  // // 更新到state
  //  router.app.$store.commit("setbreadcrumbItems", breadCrumbItems);
  // //router.app.$store.dispatch("setbreadcrumbItems", breadCrumbItems)
})


export default router